﻿/* ===============================================================================  
 * Copyright (c) 2010 Acid Frameworks
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * 
 * =============================================================================== */

using System.Linq;
using Acid.Graphics;
using System;

namespace Acid.Rendering
{
    /// <summary>
    /// Graph
    /// </summary>
    public partial class Graph : IPackObject
    {
        public static int Count { get; set; }
         

        /// <summary>
        /// The rendering Device for this Graph
        /// </summary>
        public Device          Device            { get; set;  }
        /// <summary>
        /// This Graphs Root RenderNode
        /// </summary>
        public RenderNode      RenderNode        { get; set;  }
        /// <summary>
        /// The Root Transform for this Graph. (All Transforms should be children of this Transform)
        /// </summary>
        public Transform       Transform         { get; set; }
        /// <summary>
        /// This Graphs Current Transform
        /// </summary>
        protected Transform    CurrentTransform  { get;  set; }
        /// <summary>
        /// This Graphs Current ViewInfo
        /// </summary>
        protected ViewInfo     CurrentViewInfo   { get;  set; }

        #region IPackObject Members
        /// <summary>
        /// This Graphs ID
        /// </summary>
        public string ID { get; set; }
        /// <summary>
        /// This Graphs Name
        /// </summary>
        public string Name { get; set; }
        
        #endregion

        public Graph(Device device)
        {
            this.ID         = "graph_" + Graph.Count.ToString(); Graph.Count++;
            
            this.Device     = device;
            
            this.RenderNode = null;
        }

        private void ProcessPrimitive  (Primitive primitive)
        {
            if (primitive.Material != null)
            {
                if (primitive.Material.Effect != null)
                {
                    if (primitive.Material.Effect.Shader != null)
                    {
                        primitive.Material.Effect.Shader.Begin();
                        
                        primitive.Material.Effect.Shader.Parameter("viewInfo.view",       CurrentViewInfo.View);
                        
                        primitive.Material.Effect.Shader.Parameter("viewInfo.projection", CurrentViewInfo.Projection);
                        
                        primitive.Material.Effect.Shader.Parameter("transform.local",     CurrentTransform.Local);
                        
                        primitive.Material.Effect.Shader.Parameter("transform.world",     CurrentTransform.World);
                        
                        primitive.Material.Set();
                    }
                }

                if (primitive.Material.Effect.State != null)
                {
                    this.Device.State = primitive.Material.Effect.State;
                }
            }

            if (primitive.IndexBuffer != null && primitive.VertexBuffer != null)
            {
                this.Device.IndexBuffer = primitive.IndexBuffer;
                
                this.Device.SetVertexBuffer(0, primitive.VertexBuffer);

                this.Device.Draw(Topology.Triangles);
            }

            if (primitive.Material != null)
            {
                if (primitive.Material.Effect != null)
                {
                    if (primitive.Material.Effect.Shader != null)
                    {
                        primitive.Material.Effect.Shader.End();
                         
                    }
                }
            }
        }

        private void ProcessShape      (Shape shape)
        {
            foreach (Primitive primitive in shape.Primitives)
            {
                ProcessPrimitive(primitive);
            }
        }

        private void ProcessTransform  (Transform transform)
        {
            this.CurrentTransform = transform;

            foreach (Shape shape in transform.Shapes)
            {
                ProcessShape(shape);
            }

            foreach (Transform t in transform.Transforms)
            {
                ProcessTransform(t);
            }
        }

        private void ProcessViewInfo   (ViewInfo viewInfo)
        {
            this.CurrentViewInfo = viewInfo;

            if (viewInfo.Transform != null) 
            {
                this.ProcessTransform(viewInfo.Transform);
            }
        }

        private void ProcessRenderNode (RenderNode renderNode)
        {
            this.Device.ViewPort = renderNode.ViewPort;

            if (renderNode.Active)
            {
                if (renderNode is RenderToTextureNode) 
                {
                    var renderToTextureNode = renderNode as RenderToTextureNode;

                    if (renderToTextureNode.Target != null) 
                    {
                        if (renderToTextureNode.DepthBuffer != null)
                        {
                            
                            renderToTextureNode.FrameBuffer.Begin(renderToTextureNode.Target, renderToTextureNode.DepthBuffer);
                        }
                        else
                        {
                            renderToTextureNode.FrameBuffer.Begin(renderToTextureNode.Target);
                        }
                    }
                }
                
                this.Device.Clear(renderNode.ClearColor);

                if (renderNode.ViewInfo != null) 
                {
                    this.ProcessViewInfo(renderNode.ViewInfo);
                }

                if (renderNode is RenderToTextureNode) 
                {
                    var textureRenderNode = renderNode as RenderToTextureNode;
                
                    if (textureRenderNode.Target != null)
                    {
                        textureRenderNode.FrameBuffer.End();
                    }
                }

                foreach (RenderNode node in renderNode.RenderNodes.OrderBy(n => n.Priority))
                {
                    ProcessRenderNode(node);
                }
            }
        }
       
        /// <summary>
        /// Draws the Objects in this Graph
        /// </summary>
        public  void Draw()
        {
            if (this.RenderNode != null)
            {
                this.ProcessRenderNode(this.RenderNode);
            }

            this.Device.Present();
        }

      
    }
}
